package com.mzd.multipledatasources3.controller;

import com.mzd.multipledatasources3.bean.TeachersBean;
import com.mzd.multipledatasources3.bean.TestBean;
import com.mzd.multipledatasources3.service.TransactionService1;
import com.mzd.multipledatasources3.service.TransactionService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 多数据源事务测试 配置上jta-atomikos 之后事务变得正常 无论是test、test2还是test3，两个对象都是存不进去的
 *
 * @author acer
 */
@RestController
public class TransactionController {

    @Autowired
    private TransactionService1 ts1;
    @Autowired
    private TransactionService2 ts2;

    @RequestMapping("/savetest.do")
    public Object savetest() {
        TestBean tb = new TestBean();
        tb.setId(String.valueOf(System.currentTimeMillis()));
        tb.setScore(70);
        tb.setClassId("1");
        tb.setUserId("a");
        return ts1.savetestBean(tb);
    }

    @RequestMapping("/saveteacher.do")
    public Object saveteacher() {
        TeachersBean tb = new TeachersBean();
        tb.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        tb.setTeacherName("王老师");
        tb.setClassId("1");
        return ts2.saveTeacher(tb);
    }

    // ########################开始事务测试##########################

    @RequestMapping("/test.do")
    public String test() {
        TestBean tb = new TestBean();
        tb.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        tb.setScore(70);
        tb.setClassId("1");
        tb.setUserId("a");
        ts1.savetestBean2(tb);
        return "success";
    }

    @RequestMapping("/test2.do")
    public String test2() {
        TestBean tb = new TestBean();
        tb.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        tb.setScore(70);
        tb.setClassId("1");
        tb.setUserId("a");
        ts1.savetestBean3(tb);
        return "success";
    }

    @RequestMapping("/test3.do")
    public String test3() {
        TestBean tb = new TestBean();
        tb.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        tb.setScore(70);
        tb.setClassId("1");
        tb.setUserId("a");
        ts1.savetestBean4(tb);
        return "success";
    }
}
